package sgsi.controller.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import sgsi.controller.dao.AreaDao;
import sgsi.model.entidades.espacios.Area;

public class AreaDaoHibernateImpl extends HibernateDaoSupport implements
		AreaDao {

	private static final String BUSCAR_POR_CODIGO = "select a from Area a where a.estaBorrado is false and a.codigoArea = ?";
	private static final String BUSCAR_POR_CODIGOS = "select a from Area a where a.estaBorrado is false and a.codigoArea in ";
	private static final String BUSCAR_TODOS = "select a from Area a where a.estaBorrado is false and a.nombre like ";
	private static final String ALMACEN = " a.almacen = ";
	private static final String BUSCAR_POR_CODIGO_ALMACEN = "select a from Area a where a.estaBorrado is false and a.almacen.codigoAlmacen = ?";
	private static final String BUSCAR_TODOS_LOS_ACTIVOS_DIFERENTES_A = "select a from Area a where a.estaBorrado is false and a.codigoArea != ?";
	private static final String OBTENER_TODOS = "select a from Area a where a.estaBorrado is false order by a.codigoArea";
	private static final String BUSCAR_POR_CODIGO_ALMACEN_ACTIVOS = "select a from Area a where a.estaBorrado is false and a.almacen.codigoAlmacen = ";
	private static final String DIFERENTES_A = " AND a.codigoArea != ";

	@SuppressWarnings("unchecked")
	public List<Area> obtenerTodas() {
		return getHibernateTemplate().find(OBTENER_TODOS);
	}

	public Area guardar(Area area) {
		getHibernateTemplate().saveOrUpdate(area);
		return null;
	}

	public Area obtenerPorCodigo(int codigo) {
		if (getHibernateTemplate().find(BUSCAR_POR_CODIGO, codigo).isEmpty()) {
			return null;
		}
		return (Area) getHibernateTemplate().find(BUSCAR_POR_CODIGO, codigo).get(0);
	}

	@SuppressWarnings("unchecked")
	public List<Area> obtenerPorCodigoAlmacen(int codigo) {
		return getHibernateTemplate().find(BUSCAR_POR_CODIGO_ALMACEN, codigo);
	}

	@SuppressWarnings("unchecked")
	public List<Area> obtenerTodosBusqueda(String nombre,
			int almacenSeleccionada) {
		String newQuery = BUSCAR_TODOS + "'%" + nombre + "%'";
		if (almacenSeleccionada > 0) {
			newQuery = newQuery + " AND " + ALMACEN + almacenSeleccionada;
		}
		return getHibernateTemplate().find(newQuery);
	}

	@SuppressWarnings("unchecked")
	public List<Area> obtenerPorCodigos(String[] codigos) {
		StringBuilder query = new StringBuilder(BUSCAR_POR_CODIGOS);
		query.append('(');
		for (int i = 0; i < codigos.length; i++) {
			query.append(codigos[i]);
			if (i != (codigos.length - 1)) {
				query.append(" , ");
			}
		}
		query.append(')');
		return getHibernateTemplate().find(query.toString());
	}

	@SuppressWarnings("unchecked")
	public List<Area> obtenerTodosDiferentesA(int codigo) {
		return getHibernateTemplate().find(BUSCAR_TODOS_LOS_ACTIVOS_DIFERENTES_A, codigo);
	}

	@SuppressWarnings("unchecked")
	public List<Area> obtenerPorCodigoAlmacenDiferentesA(int codigoAlmacen, int codigoArea) {
		StringBuilder query = new StringBuilder(BUSCAR_POR_CODIGO_ALMACEN_ACTIVOS + codigoAlmacen + DIFERENTES_A + codigoArea);
		return getHibernateTemplate().find(query.toString());
	}
}
